using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SceneryEditor
{
    /// <summary>
    /// This is the main form and the entry point of the application. All other forms braches from here.
    /// </summary>
    public partial class MainForm : Form
    {
        /// <summary>
        /// Forms ids
        /// </summary>
        public enum FRM
        {
            None = 0,
            SceneryEditor = 1,
        }

        public FrmSceneryEditor frmSceneryEditor = null;

        public delegate void FormOpenHandler(Form sender, FRM frmId);

        public MainForm()
        {
            InitializeComponent();
        }

        public void InitializeApplication()
        {
            // DO WHATEVER APPROPIATE HERE AFTER THE APPLICATION STARTS

            FormOpen(this, FRM.SceneryEditor);
            this.WindowState = FormWindowState.Maximized;
        }

        // this method is used when as a request to open a form is open. So tackle some events and set some properites for this particular form
        private void FormOpen(Form sender, FRM frmId)
        {
            // The scenery editor
            if (frmId == FRM.SceneryEditor)
            {
                if (frmSceneryEditor == null || frmSceneryEditor.closed)
                {
                    frmSceneryEditor = new FrmSceneryEditor();
                    frmSceneryEditor.MdiParent = this;
                    frmSceneryEditor.OnExit += Exit;
                    frmSceneryEditor.Show();
                    frmSceneryEditor.BringToFront();
                }
                else
                {
                    frmSceneryEditor.BringToFront();
                }
            }
        }

        #region "Main loop"
        /// <summary>
        /// This method starts the main loop.
        /// </summary>
        public void MainLoop()
        {
            // We'll hook the application's idle event
            Application.Idle += new EventHandler(this.OnApplicationIdle);

            // Go!
            Application.Run(this);
        }

        /// <summary>
        /// Handles the Idle event of the Application class.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void OnApplicationIdle(object sender, EventArgs e)
        {
            // If the application windows is not focused, let's pause the aplication.
            //if (!this.Focused)
            //{
            //    return;
            //}
            //while (!msgPeeker.MessageAvailable)
            //{
            //   
            //}
        }
        #endregion

        // Finalize the program
        private void Exit(object sender, EventArgs e)
        {
            // DO WHATEVER APPROPIATE HERE BEFORE EXIT THE APPLICATION

            this.Dispose();
        }
    }
}